package com.mk.dao.config;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.mk.dao.domain.AbstractBaseModel;
import com.mk.dao.mapper.CommonMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @author lin-mt
 */
public class CodeGenerateUtil {

    private static final String SOURCE_PATH = "/src/main/java";
    private static final String XML_PATH = "/src/main/resources/mapper";
    private static final String DAO_OUT_PATH = System.getProperty("user.dir") + "/" + "sch-server";

    private static final String URL =
            "jdbc:mysql://localhost:3306/sch-cat?useUnicode=true&characterEncoding=UTF-8"
                    + "&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
    private static final String USER_NAME = "root";
    private static final String PASSWORD = "123456";

    public static void main(String[] args) {
        List<String> tables = getGenerateTables();
        DataSourceConfig dataSourceConfig =
                new DataSourceConfig.Builder(URL, USER_NAME, PASSWORD)
                        .schema("sch-cat")
                        .keyWordsHandler(new MySqlKeyWordsHandler())
                        .build();
        Map<OutputFile, String> pathInfos = new HashMap<>();
        pathInfos.put(OutputFile.xml, DAO_OUT_PATH + XML_PATH);
        pathInfos.put(OutputFile.controller, null);
        GlobalConfig globalConfig =
                new GlobalConfig.Builder()
                        .outputDir(DAO_OUT_PATH + SOURCE_PATH)
                        .author("mybatis-generate")
                        .disableOpenDir()
                        .dateType(DateType.TIME_PACK)
                        .commentDate("yyyy-MM-dd")
                        .build();
        PackageConfig packageConfig =
                new PackageConfig.Builder()
                        .parent("com.mk")
                        .entity("dao.domain")
                        .service("dao.interfaces")
                        .serviceImpl("dao.interfaces.impl")
                        .mapper("dao.mapper")
                        .pathInfo(pathInfos)
                        .build();
        StrategyConfig strategyConfig =
                new StrategyConfig.Builder()
                        .enableSkipView()
                        .disableSqlFilter()
                        .addInclude(tables)
                        .entityBuilder()
                        .superClass(AbstractBaseModel.class)
                        .disableSerialVersionUID()
                        .enableChainModel()
                        .enableLombok()
                        .enableActiveRecord()
                        .naming(NamingStrategy.underline_to_camel)
                        .columnNaming(NamingStrategy.underline_to_camel)
                        .enableFileOverride()
                        // Mapper
                        .mapperBuilder()
                        .superClass(CommonMapper.class)
                        .enableFileOverride()
                        // Service
                        .serviceBuilder()
                        .formatServiceFileName("%sDao")
                        .formatServiceImplFileName("%sDaoImpl")
                        .build();

        new AutoGenerator(dataSourceConfig)
                .global(globalConfig)
                .packageInfo(packageConfig)
                .strategy(strategyConfig)
                .execute();
    }

    public static List<String> getGenerateTables() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要生成的表名称（两次回车确认）：");
        List<String> tableNames = new ArrayList<>();
        while (scanner.hasNextLine()) {
            String tableName = scanner.nextLine();
            if (StringUtils.isBlank(tableName)) {
                return tableNames;
            }
            if (StringUtils.isNotBlank(tableName)) {
                tableNames.add(tableName);
            }
        }
        throw new MybatisPlusException("请输入正确的表名！");
    }
}
